package com.ouheng.shop.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ouheng.shop.domain.ProductDO;
import com.ouheng.shop.domain.SexualityDO;
import com.ouheng.shop.service.ProductService;
import com.ouheng.shop.service.SexualityService;
import com.ouheng.common.config.Constant;
import com.ouheng.common.utils.PageUtils;
import com.ouheng.common.utils.Query;
import com.ouheng.common.utils.R;

/**
 * 
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-09-19 17:13:30
 */
 
@Controller
@RequestMapping("/shop/sexuality")
public class SexualityController {
	@Autowired
	private SexualityService sexualityService;
	
	@Autowired
	private ProductService productService;
	
	@GetMapping()
	@RequiresPermissions("shop:sexuality:sexuality")
	String Sexuality(){
	    return "shop/sexuality/sexuality";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("shop:sexuality:sexuality")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<SexualityDO> sexualityList = sexualityService.list(query);
		int total = sexualityService.count(query);
		PageUtils pageUtils = new PageUtils(sexualityList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("shop:sexuality:add")
	String add(Model model){
		List<ProductDO> listPreProduct = productService.list(null);
		model.addAttribute("listPreProduct", listPreProduct);
		model.addAttribute("Oss_Url", Constant.Oss_Url);
	    return "shop/sexuality/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("shop:sexuality:edit")
	String edit(@PathVariable("id") Long id,Model model){
		SexualityDO sexuality = sexualityService.get(id);
		List<ProductDO> listPreProduct = productService.list(null);
		model.addAttribute("listPreProduct", listPreProduct);
		model.addAttribute("sexuality", sexuality);
		model.addAttribute("Oss_Url", Constant.Oss_Url);
	    return "shop/sexuality/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("shop:sexuality:add")
	public R save( SexualityDO sexuality){
		sexuality.setSort(0);
		sexuality.setStatus("1");
		sexuality.setCreatedate(new Date());
		sexuality.setModifydate(new Date());
		if(sexualityService.save(sexuality)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("shop:sexuality:edit")
	public R update( SexualityDO sexuality){
		sexualityService.update(sexuality);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("shop:sexuality:remove")
	public R remove( Long id){
		if(sexualityService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("shop:sexuality:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		sexualityService.batchRemove(ids);
		return R.ok();
	}
	
	/**
	 * 发布
	 * @Title: changeJobStatus   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @param id
	 * @param: @param cmd
	 * @param: @return      
	 * @return: R      
	 * @throws
	 */
	@PostMapping(value = "/changeStatus")
	@ResponseBody
	public R changeJobStatus(Long id,String cmd) {
		String status="";
		String label = "取消发布";
		if ("start".equals(cmd)) {
			status="2";
			label = "发布";
		} else {
			status="1";
			label = "取消发布";
		}
		try {
			SexualityDO sexualityDO = sexualityService.get(id);
			sexualityDO.setStatus(status);
			sexualityService.update(sexualityDO);
			return R.ok(label + "成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return R.ok("任务" + label + "失败");
	}
	/**
	 * 置顶
	 * @Title: setTop   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @param id
	 * @param: @return      
	 * @return: R      
	 * @throws
	 */
	@GetMapping(value = "/setTop")
	@ResponseBody
	public R setTop(Long id) {
		try {
			int sort = sexualityService.maxSort();
			SexualityDO sexualityDO = sexualityService.get(id);
			if(sexualityDO.getSort()!=sort){
				sexualityDO.setSort(sort+1);
				sexualityDO.setModifydate(new Date());
				sexualityService.update(sexualityDO);
			}
			return R.ok();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return R.ok();
	}
}
